package com.smartstate.IOC.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.IOC.dto.EnergyDTO;
import com.smartstate.IOC.dto.PageDTO;
import com.smartstate.IOC.entity.ObjMoudel;
import com.smartstate.IOC.mapper.ObjMoudelMapper;
import com.smartstate.IOC.service.ObjMoudelService;
import com.smartstate.IOC.vo.ObjMoudelTwoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.swing.tree.TreeNode;
import java.io.File;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>楼宇</p>
 * <p>@Author 陈永杰</p>
 * <p>@Date: 2022/8/17</p>
 * <p>@Time: 22:06</p>
 * <p>@version 1.0 <p>
 */
@Transactional
@Service
@Slf4j
public class ObjMoudelServiceImpl extends ServiceImpl<ObjMoudelMapper, ObjMoudel> implements ObjMoudelService {

    @Resource
    private ObjMoudelMapper objMoudelMapper;

    @Value("${environmental.monitoring}")
    private String url;



    //查出所有父子级
    @Override
    public List<Map<String, String>> selectMetering(EnergyDTO item) {
        Map mapElect = new HashMap();
        List<Map<String, String>> children = new ArrayList<>();
        String electricitys = null;
        String waters = null;
        String naturals = null;
        String steams = null;

        ObjMoudelTwoVo objMoudelTwoVo = objMoudelMapper.selectMetering(item);
        if (objMoudelTwoVo != null) {
            List<Map> lists = JSON.parseArray(objMoudelTwoVo.getMetering(), Map.class);
            if (lists != null) {
                for (Map mapList : lists) {
                    if (mapList.get("电") != null && !mapList.get("电").equals("")) {
                        electricitys = (String) mapList.get("电");
                        mapElect.put("电", electricitys);

                    } else if (mapList.get("水") != null && !mapList.get("水").equals("")) {
                        waters = (String) mapList.get("水");
                        mapElect.put("水", waters);
                    } else if (mapList.get("天然气") != null && !mapList.get("天然气").equals("")) {
                        naturals = (String) mapList.get("天然气");
                        mapElect.put("天然气", naturals);
                    } else if (mapList.get("蒸汽") != null && !mapList.get("蒸汽").equals("")) {
                        steams = (String) mapList.get("蒸汽");
                        mapElect.put("蒸汽", steams);
                    }

                }
            }
            mapElect.put("id", objMoudelTwoVo.getId());
            mapElect.put("name", objMoudelTwoVo.getName());
            mapElect.put("parentId", objMoudelTwoVo.getParentId());
            mapElect.put("type", objMoudelTwoVo.getType());
//            mapElect.put("children", children);
            children.add(mapElect);
            metering(children, objMoudelTwoVo.getId());
        }
        return children;
    }

    //组装树
    @Override
    public Map<String, Object> selectPowerTree(EnergyDTO item) {
        Map mapElect = new HashMap();
        List<Map<String, Object>> children = new ArrayList<>();
        String electricitys = null;
        String waters = null;
        String naturals = null;
        String steams = null;

        ObjMoudelTwoVo objMoudelTwoVo = objMoudelMapper.selectMetering(item);
        List<Map> lists = JSON.parseArray(objMoudelTwoVo.getMetering(), Map.class);
        if (lists != null) {
            for (Map mapList : lists) {
                if (mapList.get("电") != null && !mapList.get("电").equals("")) {
                    electricitys = (String) mapList.get("电");
                    mapElect.put("电", electricitys);

                } else if (mapList.get("水") != null && !mapList.get("水").equals("")) {
                    waters = (String) mapList.get("水");
                    mapElect.put("水", waters);
                } else if (mapList.get("天然气") != null && !mapList.get("天然气").equals("")) {
                    naturals = (String) mapList.get("天然气");
                    mapElect.put("天然气", naturals);
                } else if (mapList.get("蒸汽") != null && !mapList.get("蒸汽").equals("")) {
                    steams = (String) mapList.get("蒸汽");
                    mapElect.put("蒸汽", steams);
                }

            }
        }
        mapElect.put("id", objMoudelTwoVo.getId());
        mapElect.put("name", objMoudelTwoVo.getName());
        mapElect.put("parentId", objMoudelTwoVo.getParentId());
        mapElect.put("type", objMoudelTwoVo.getType());
        mapElect.put("children", children);
        meteringTree(mapElect, objMoudelTwoVo.getId());

        return mapElect;
    }

    private void metering(List<Map<String, String>> children, String id) {
        String electricitys = null;
        String waters = null;
        String naturals = null;
        String steams = null;
        //查询第一层的根节点  下的子级
        List<ObjMoudelTwoVo> objMoudelTwoVos = objMoudelMapper.selectMeteringAll(id);
        //如果下面没有子级 就返回
        if (objMoudelTwoVos.size() != 0) {
            //如果根节点有子级 就递归遍历 找下面的子级 自己调自己方法
            for (ObjMoudelTwoVo objMoudelTwoVo : objMoudelTwoVos) {
                objMoudelTwoVo.getId();
                objMoudelTwoVo.getName();
                objMoudelTwoVo.getParentId();
                Map mapElect = new HashMap();

                List<Map> lists = JSON.parseArray(objMoudelTwoVo.getMetering(), Map.class);
                if (lists != null) {
                    for (Map mapList : lists) {
                        if (mapList.get("电") != null && !"".equals(mapList.get("电"))) {
                            electricitys = (String) mapList.get("电");
                            mapElect.put("电", electricitys);
                        } else if (mapList.get("水") != null && !"".equals(mapList.get("水"))) {
                            waters = (String) mapList.get("水");
                            mapElect.put("水", waters);
                        } else if (mapList.get("天然气") != null && !"".equals(mapList.get("天然气"))) {
                            naturals = (String) mapList.get("天然气");
                            mapElect.put("天然气", naturals);
                        } else if (mapList.get("蒸汽") != null && !"".equals(mapList.get("蒸汽"))) {
                            steams = (String) mapList.get("蒸汽");
                            mapElect.put("蒸汽", steams);
                        }

                    }
                }
                mapElect.put("id", objMoudelTwoVo.getId());
                mapElect.put("name", objMoudelTwoVo.getName());
                mapElect.put("parentId", objMoudelTwoVo.getParentId());
                mapElect.put("type", objMoudelTwoVo.getType());
//                mapElect.put("children", children);
                children.add(mapElect);
//                List<Map<String, ObjMoudel>> list = (List<Map<String, ObjMoudel>>)map.get("children");
//                list.add(mapElect);
                metering(children, objMoudelTwoVo.getId());
            }
        }

    }



    private void meteringTree(Map map, String id) {
        String electricitys = null;
        String waters = null;
        String naturals = null;
        String steams = null;
        //查询第一层的根节点  下的子级
        List<ObjMoudelTwoVo> objMoudelTwoVos = objMoudelMapper.selectMeteringAll(id);
        //如果下面没有子级 就返回
        if (objMoudelTwoVos.size() != 0) {
            //如果根节点有子级 就递归遍历 找下面的子级 自己调自己方法
            for (ObjMoudelTwoVo objMoudelTwoVo : objMoudelTwoVos) {
                List<Map<String, Object>> children = new ArrayList<>();
                objMoudelTwoVo.getId();
                objMoudelTwoVo.getName();
                objMoudelTwoVo.getParentId();
                Map mapElect = new HashMap();
                List<Map> lists = JSON.parseArray(objMoudelTwoVo.getMetering(), Map.class);
                if (lists != null) {
                    for (Map mapList : lists) {
                        if (mapList.get("电") != null && !mapList.get("电").equals("")) {
                            electricitys = (String) mapList.get("电");
                            mapElect.put("电", electricitys);

                        } else if (mapList.get("水") != null && !mapList.get("水").equals("")) {
                            waters = (String) mapList.get("水");
                            mapElect.put("水", waters);
                        } else if (mapList.get("天然气") != null && !mapList.get("天然气").equals("")) {
                            naturals = (String) mapList.get("天然气");
                            mapElect.put("天然气", naturals);
                        } else if (mapList.get("蒸汽") != null && !mapList.get("蒸汽").equals("")) {
                            steams = (String) mapList.get("蒸汽");
                            mapElect.put("蒸汽", steams);
                        }

                    }
                }
                mapElect.put("id", objMoudelTwoVo.getId());
                mapElect.put("name", objMoudelTwoVo.getName());
                mapElect.put("parentId", objMoudelTwoVo.getParentId());
                mapElect.put("type", objMoudelTwoVo.getType());
                mapElect.put("children", children);
                List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("children");
                list.add(mapElect);
                meteringTree(mapElect, objMoudelTwoVo.getId());
            }
        }

    }


    @Override
    public Page<ObjMoudel> queryAllPageByDTO(PageDTO pageDTO) {
        return null;
    }
}